﻿package com.reyco1.manager
{
	/*
	imports
	*/
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.ui.ContextMenuBuiltInItems;
    import flash.events.ContextMenuEvent;
	import flash.display.DisplayObject;
	import com.reyco1.util.ProxyUtil;
	import com.reyco1.extension.DispatchExtension;
	import com.reyco1.events.ContextMenuManagerEvent;
	/*
	class
	*/
    public class ContextMenuManager extends DispatchExtension
	{
		/*
		variables
		*/
        private var myContextMenu:ContextMenu;
		private var arrMenuItems:Array;
		private var objScope:*;
		/*
		class
		*/
		
        /**
         * Applies custome menu items to the right click context meny of any display object including the stage.
         * @param $scope : The display object to which you wish to add th custom context menu to.
         * 
         */		
        public function ContextMenuManager($scope:DisplayObject):void
		{
            this.myContextMenu = new ContextMenu();
			this.objScope = $scope;
			this.arrMenuItems = new Array();
			this.removeDefaultItems();
            this.myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, this.menuSelectHandler);
            this.objScope.contextMenu = this.myContextMenu;
        }
		/*
		functions
		*/
		
		// handler for when the right-right click is made
		private function menuSelectHandler(event:ContextMenuEvent):void 
		{
            this.removeDefaultItems();
			if(event.mouseTarget == this.objScope){
				this.addCustomMenuItems();
			}
			this.dispatchEvent(new ContextMenuManagerEvent(ContextMenuManagerEvent.RIGHT_CLICK, {}))
		
        }		
		
		// remove all context menu items both custom and built in
        private function removeDefaultItems():void 
		{
            this.myContextMenu.hideBuiltInItems();
           	this.myContextMenu.customItems = [];
        }
		
		// adds context menu ites based on the interactive or display object which was right clicked
		private function addCustomMenuItems():void 
		{
			if(this.arrMenuItems.length > 0){
				for(var a:uint = 0; a<this.arrMenuItems.length; a++){
					this.addSingleCustomItem(this.arrMenuItems[a].label, this.arrMenuItems[a].separate, this.arrMenuItems[a].enabled);
				}
			}
        }
		
		// add a custom item to the context menu
		private function addSingleCustomItem($label:String, $separatorBefore:Boolean = false, $enabled:Boolean = true):void
		{
			var tempItem:ContextMenuItem = new ContextMenuItem($label, $separatorBefore, $enabled);
			this.myContextMenu.customItems.push(tempItem);
			tempItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, ProxyUtil.create(this.menuItemSelectHandler, $label));
		}
		
		/**
		 * Adds a custom menu item to the right click menu
		 * @param $label : the menu label
		 * @param $separatorBefore : Boolean which determines if a separator should be added before this particular label
		 * @param $enabled : determines if th emenu item is enabled or not
		 * 
		 */		
		public function addMenuItem($label:String, $separatorBefore:Boolean = false, $enabled:Boolean = true):void
		{
			 this.removeDefaultItems();
			 this.arrMenuItems.push({label:$label, separate:$separatorBefore, enabled:$enabled});
			 this.addCustomMenuItems();
		}
		
		/**
		 * Removes a custom menu item from the right click menu
		 * @param $label
		 * 
		 */		
		public function removeMenuItem($label:String):void
		{
			var arrTemp:Array = new Array();
			for(var a:uint = 0; a<this.arrMenuItems.length; a++){
				if(this.arrMenuItems[a].label != $label){
					arrTemp.push(this.arrMenuItems[a])
				}
			}
			this.arrMenuItems = arrTemp;
			this.addCustomMenuItems();
		}
		
		/**
		 * Toggles the "enabled" property of the indicated context menu item
		 * @param $label : label of the menu item to toggle
		 * @param $enabled : boolean specifying if the menu item is enabled or not
		 * 
		 */		
		public function menuItemEnabled($label:String, $enabled:Boolean):void
		{
			this.removeDefaultItems();
			for(var a:uint = 0; a<this.arrMenuItems.length; a++){
				if(this.arrMenuItems[a].label != $label){
					this.arrMenuItems[a].enabled = $enabled;
				}
			}
			this.addCustomMenuItems();
		}
		
		// handler for the context menu item selected
		private function menuItemSelectHandler(event:ContextMenuEvent, $menuItem:String):void 
		{
          this.dispatchEvent(new ContextMenuManagerEvent(ContextMenuManagerEvent.MENU_SELECTED, {label:$menuItem, target:this.objScope}))
		}
		
    }
}